//
//  LoginView.m
//  LoginViewDemo
//
//  Created by admin on 2019/6/18.
//  Copyright © 2019 薛磊. All rights reserved.
//

#import "LoginView.h"
#import <ReactiveObjC/ReactiveObjC.h>
#import <Masonry/Masonry.h>
#import "UIImage+Color.h"


#define XLRGB(r,g,b) [UIColor colorWithRed:(r/255.0) green:(g/255.0) blue:b/255.0 alpha:1]

@interface  LoginView()<UITextFieldDelegate>
{
    int countdown;
    
}
@property(nonatomic,strong)LoginViewConfigure * configure;

@property(nonatomic,strong)UILabel * topLabel;


@property(nonatomic,strong)UIButton * codeBt;
@property(nonatomic,strong)UIView * phoneLineView;
@property(nonatomic,strong)UIView * codeLineView;

@property(nonatomic,strong)UIView * lineView;

@property(nonatomic,strong)UIButton * loginBt;

@property(nonatomic,strong)UIButton * userBt;

@property(nonatomic,strong)UILabel * agreeLabel;

@property(nonatomic,strong)dispatch_source_t timer;


@end

@implementation LoginView


- (instancetype)initWithFrame:(CGRect)frame configure:(LoginViewConfigure *)configure
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.configure = configure;
        
        [self configureView];
        
        countdown = 60;//计时器
        
        [self setupView];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        countdown = 60;//计时器
        
        [self configureView];
        
        [self setupView];
    }
    return self;
}
-(void)configureView{
    
    RAC(self.loginBt,enabled) = [[RACSignal combineLatest:@[self.phoneTF.rac_textSignal,self.codeTF.rac_textSignal]] map:^id _Nullable(RACTuple * _Nullable value) {
        
        RACTupleUnpack(NSString * phone,NSString * sms) = value;
        
        return @(phone.length >=11 && sms.length >=6);
        
    }];
    
}
-(void)setupView{
    
    
    self.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:self.topLabel];
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(45);
        make.top.mas_equalTo(148);
        
    }];
    [self.codeBt addTarget:self action:@selector(codeClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.codeBt];
    [self.codeBt mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(-58);
        make.top.mas_equalTo(self.topLabel.mas_bottom).offset(56);
        
        
    }];
    
    [self addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self.codeBt.mas_left).offset(-10);
        make.centerY.mas_equalTo(self.codeBt.mas_centerY);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(18);
        
    }];
    
    [self addSubview:self.phoneTF];
    [self.phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(45);
        make.centerY.mas_equalTo(self.codeBt.mas_centerY);
        make.height.mas_equalTo(21);
        make.width.mas_equalTo(200);
    }];
    
    [self addSubview:self.phoneLineView];
    [self.phoneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(-45);
        make.left.mas_equalTo(45);
        make.top.mas_equalTo(self.phoneTF.mas_bottom).offset(10);
        make.height.mas_equalTo(1);
        
    }];
    
    [self addSubview:self.codeTF];
    [self.codeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(-45);
        make.left.mas_equalTo(45);
        make.top.mas_equalTo(self.phoneLineView.mas_bottom).offset(30);
        make.height.mas_equalTo(21);
        
    }];
    
    [self addSubview:self.codeLineView];
    [self.codeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(-45);
        make.left.mas_equalTo(45);
        make.top.mas_equalTo(self.codeTF.mas_bottom).offset(10);
        make.height.mas_equalTo(1);
        
    }];
    [self.loginBt addTarget:self action:@selector(touchLoginBut:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:self.loginBt];
    [self.loginBt mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(-45);
        make.left.mas_equalTo(45);
        make.top.mas_equalTo(self.codeLineView.mas_bottom).offset(70);
        
    }];
    
    [self addSubview:self.agreeLabel];
    [self.agreeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self.mas_centerX).offset(-50);
        make.top.mas_equalTo(self.loginBt.mas_bottom).offset(20);
        
    }];
    @weakify(self);
    [[self.userBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        if (self.delegate && [self.delegate respondsToSelector:@selector(userProtocol:)]) {
            [self.delegate userProtocol:self];
            
        }
        
    }];
    [self addSubview:self.userBt];
    [self.userBt mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.agreeLabel.mas_right);
        make.centerY.mas_equalTo(self.agreeLabel.mas_centerY);
        
    }];
    
    
    
    
}
#pragma mark -获取验证码-
-(void)codeClick{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(obtainSms:)]) {
        
        [self.delegate obtainSms:self];
        
    }
    
}
-(void)touchLoginBut:(UIButton *)sender{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(loginViewLogin:)]) {
        
        [self.delegate loginViewLogin:self];
        
    }
    
}


-(void)begGcd{
    
    if (countdown !=0) {
        
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        
        self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
        
        dispatch_source_set_timer(self.timer,DISPATCH_TIME_NOW, 1*NSEC_PER_SEC, 0);
        
        dispatch_source_set_event_handler(self.timer, ^{
            
            
            if (self->countdown <=0) {
                
                dispatch_source_cancel(self.timer);
                dispatch_async(dispatch_get_main_queue(), ^{
                    //设置界面的按钮显示 根据自己需求设置
                    self->countdown = 60;
                    self.codeBt.userInteractionEnabled = YES;
                    [self.codeBt setTitle:@"重新获取" forState:UIControlStateNormal];
                    
                });
            }else{//做一些相关操作
                
                self->countdown--;
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    self.codeBt.userInteractionEnabled = NO;
                    [self.codeBt setTitle:[NSString stringWithFormat:@"%d",self->countdown] forState:UIControlStateNormal];
                    
                });
                
            }
        });
        dispatch_resume(self.timer);
        
    }
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if ([string isEqualToString:@"\n"]) return YES;
    
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
    if (self.phoneTF == textField) {
        if ([toBeString length] > 11 ) {
            
            textField.text = [toBeString substringToIndex:11];
            
            return NO;
        }else{
            return YES;
        }
    }else{
        if ([toBeString length] > 6 ) {
            
            textField.text = [toBeString substringToIndex:6];
            
            return NO;
        }else{
            return YES;
        }
        
        
    }
}
-(UILabel *)topLabel{
    if (!_topLabel) {
        
        _topLabel = [UILabel new];
        
        _topLabel.text = @"手机快捷登录";
        
        _topLabel.font = self.configure.topFontSize ? [UIFont fontWithName:@"PingFangSC-Medium" size: self.configure.topFontSize] : [UIFont fontWithName:@"PingFangSC-Medium" size: 20];
        
    }
    return _topLabel;
}
-(UILabel *)agreeLabel{
    if (!_agreeLabel) {
        
        _agreeLabel = [UILabel new];
        
        _agreeLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size: 12];
        
        _agreeLabel.text = @"登录即代表已同意";
    }
    return _agreeLabel;
}
-(UITextField *)phoneTF{
    
    if (!_phoneTF) {
        
        _phoneTF = [[UITextField alloc] init];
    
        _phoneTF.placeholder = self.configure.phoneTfPlaceholdText ? self.configure.phoneTfPlaceholdText : @"请输入手机号";
        
        _phoneTF.tintColor = XLRGB(162, 162, 162);

        _phoneTF.font = [UIFont fontWithName:@"PingFangSC-Regular" size: 15];
        
        _phoneTF.keyboardType = UIKeyboardTypeNumberPad;
        
        _phoneTF.delegate = self;
    }
    
    
    return _phoneTF;
}
-(UITextField *)codeTF{
    
    if (!_codeTF) {
        
        _codeTF = [[UITextField alloc] init];
        
        _codeTF.placeholder = @"请输入短信验证码";
        
        _codeTF.tintColor = XLRGB(162, 162, 162);
        
        _codeTF.font = [UIFont fontWithName:@"PingFangSC-Regular" size: 15];
        
        _codeTF.delegate = self;
        
        _codeTF.keyboardType = UIKeyboardTypeNumberPad;
        
    }
    
    
    return _codeTF;
}
-(UIView *)lineView{
    
    if (!_lineView) {
        
        _lineView = [UIView new];
        
        _lineView.backgroundColor = XLRGB(220, 220, 220);
    }
    
    
    return _lineView;
}
-(UIView *)phoneLineView{
    
    if (!_phoneLineView) {
        
        _phoneLineView = [UIView new];
        
        _phoneLineView.backgroundColor = XLRGB(220, 220, 220);
    }
    
    
    return _phoneLineView;
}
-(UIView *)codeLineView{
    
    if (!_codeLineView) {
        
        _codeLineView = [UIView new];
        
        _codeLineView.backgroundColor = XLRGB(220, 220, 220);
    }
    
    
    return _codeLineView;
}
-(UIButton *)codeBt{
    
    if (!_codeBt) {
        
        _codeBt = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_codeBt setTitle:@"获取验证码" forState:UIControlStateNormal];
        
        [_codeBt setTitleColor:XLRGB(24, 105, 255) forState:UIControlStateNormal];
        
        _codeBt.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size: 15];
        
    }
    
    return _codeBt;
    
}
-(UIButton *)userBt{
    
    if (!_userBt) {
        
        _userBt = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_userBt setTitle:@"《用户协议》" forState:UIControlStateNormal];
        
        [_userBt setTitleColor:XLRGB(24, 105, 255) forState:UIControlStateNormal];
        
        _userBt.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size: 12];
        
    }
    
    return _userBt;
    
}
-(UIButton *)loginBt{
    
    if (!_loginBt) {
        
        _loginBt = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_loginBt setTitle:@"登录" forState:UIControlStateNormal];
        
        _loginBt.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size: 18];
        
        [_loginBt setBackgroundImage:[UIImage jp_imageWithColor:XLRGB(79, 116, 246) size:CGSizeMake([UIScreen mainScreen].bounds.size.width - 90, 40) radius:20] forState:UIControlStateNormal];
       
        [_loginBt setBackgroundImage:[UIImage jp_imageWithColor:[UIColor colorWithRed:166/255.0 green:185/255.0 blue:250/255.0 alpha:1] size:CGSizeMake([UIScreen mainScreen].bounds.size.width - 90, 40) radius:20] forState:UIControlStateDisabled];
        
    }
    
    return _loginBt;
    
}
-(UIImage *)imageNamedFromJPHUDBundle:(NSString*)name{
    
    NSBundle *bundle = [NSBundle bundleForClass:[LoginView class]];
    
    NSURL *url = [bundle URLForResource:@"LoginView" withExtension:@"bundle"];
    bundle = [NSBundle bundleWithURL:url];
    
    name = [name stringByAppendingString:@"@2x"];
    NSString *imagePath = [bundle pathForResource:name ofType:@"png"];
    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
    if (!image) {
        // 兼容业务方自己设置图片的方式
        name = [name stringByReplacingOccurrencesOfString:@"@2x" withString:@""];
        image = [UIImage imageNamed:name];
    }
    
    return image?:[UIImage new];
    
}

@end

@implementation LoginViewConfigure


@end

